package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param order
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order) {

        log.info("订单数据：{}", order);

        orderService.submit(order);

        return R.success("下单成功!");
    }

    /**
     * 查询用户订单 分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> page(int page, int pageSize) {

        log.info("page: {}, pageSize: {}", page, pageSize);

        // 分页构造器
        Page<OrdersDto> pageInfo = new Page<>(page, pageSize);
        Page<Orders> ordersPage = new Page<>(page, pageSize);

        // 条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);

        // 执行分页查询
        orderService.page(ordersPage, queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(ordersPage, pageInfo, "records");
        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> list = ordersList.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            // 进行对象拷贝
            BeanUtils.copyProperties(item, ordersDto);
            /*// 设置用户名
            ordersDto.setUserName(item.getUserName());
            // 设置电话号码
            ordersDto.setPhone(item.getPhone());
            // 设置详细地址
            ordersDto.setAddress(item.getAddress());
            // 设置收货人
            ordersDto.setConsignee(item.getConsignee());*/

            // 根据订单 id 来查询 详细订单信息
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);

            ordersDto.setOrderDetails(orderDetailList);

            return ordersDto;
        }).collect(Collectors.toList());

        pageInfo.setRecords(list);

        return R.success(pageInfo);
    }

    /**
     * 管理端查询所有订单 - 分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> orderPage(int page, int pageSize, String number, @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        log.info("page: {}, pageSize: {}", page, pageSize);

        // 分页构造器
        Page<OrdersDto> pageInfo = new Page<>(page, pageSize);
        Page<Orders> ordersPage = new Page<>(page, pageSize);

        // 条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        // queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.eq(number != null, Orders::getNumber, number);
        queryWrapper.ge(beginTime != null, Orders::getOrderTime, beginTime);
        queryWrapper.le(endTime != null, Orders::getOrderTime, endTime);

        // 执行分页查询
        orderService.page(ordersPage, queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(ordersPage, pageInfo, "records");
        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> list = ordersList.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            // 进行对象拷贝
            BeanUtils.copyProperties(item, ordersDto);

            // 根据订单 id 来查询 详细订单信息
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);

            ordersDto.setOrderDetails(orderDetailList);

            return ordersDto;
        }).collect(Collectors.toList());

        pageInfo.setRecords(list);

        return R.success(pageInfo);
    }

}
